This demo constructs a $3\times 3$ QR factorization using Householder reflectors.
import numpy as np
import numpy.linalg as la
n = 3
e1 = np.array([1,0,0])
e2 = np.array([0,1,0])
e3 = np.array([0,0,1])
A = np.random.randn(n, n)
A
Householder reflector:
$$I-2\frac{vv^T}{v^Tv}$$Choose $v=a-\|a\|e_1$.
#clear
a = A[:, 0]
v = a-la.norm(a)*e1
H1 = np.eye(3) - 2*np.outer(v, v)/(v@v)
A1 = H1 @ A
A1
NB: Never build full Householder matrices in actual code! (Why? How?)
#clear
a = A1[:, 1].copy()
a[0] = 0
v = a-la.norm(a)*e2
H2 = np.eye(3) - 2*np.outer(v, v)/(v@v)
R = H2 @ A1
R
Q = np.dot(H2, H1).T
la.norm(np.dot(Q, R) - A)